<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/**
 * 帮助控制器
 * @author LM
 *
 */
class Helpcenter extends CI_Controller{
	
	public function index(){

		redirect("http://ekong.kf5.com");
		die;
		$this->load->library('user_agent');
		$cache_key = "help-index-".($this->agent->is_mobile()?"mobile":"pc");
		$this->load->driver('cache',config_item("view_cache_handle"));
		//var_dump($cache_key);exit;
		if ($cache_data = $this->cache->get($cache_key))
		{
			echo $cache_data;
			return TRUE;
		}

		//$data['active'] = $this->input->get('active');
		$this->load->database();
		$result = $this->db->get('helpcenter')->result_array();
		require_once(APPPATH."third_party/html/Simple_html_dom.php");
		$list=array();
		$html = new simple_html_dom();
		foreach($result as $k=>$v)
		{

			$html->load($v['content']);
			$table = $html->find('table');
			if($table)
			{
				/*echo "<pre>";
				var_export($v['content']);
				echo "<br/>";*/
				$temp=array();
				foreach($table as $table_el)
				{
					$tr = $table_el->find("tr");
					if($tr)
					{
						$tag=0;
						foreach($tr as $tr_key=>$tr_el)
						{
							if($tr_el->class!="firstRow")
							{
								$td=$tr_el->find("td");
								if($td)
								{
									//var_dump($td[0]->rowspan);
									if(count($td)>2)
									{
										$tag=$tr_key;
									}
									if(!isset($temp[$tag]) || isset($temp[$tag]['title']))
									{
										$temp[$tag]['title']=$this->get_text($td,0);
									}
									if(count($td)<=2)
									{
										$temp[$tag]['item'][]=array($this->get_text($td,0),$this->get_text($td,1));
									}
									else
									{
										$temp[$tag]['item'][]=array($this->get_text($td,1),$this->get_text($td,2));
									}
								}
							}
						}
					}
				}
				$v['item']=array_values($temp);
				$v['table']=!empty($temp);
				$list[]=$v;
			}
			else
			{
				$v['item']="";
				$v['table']=false;
				$list[]=$v;
			}

			$html->clear();
		}
		//echo json_encode($list);return;
		$data['help_data']=$list;
		//PC端
		if(!$this->agent->is_mobile())
		{
			$cache_data = $this->template->view('helpcenter/helpcenter.tpl', $data,TRUE);
			$this->cache->save($cache_key, $cache_data, config_item("view_cache_time"));
		}
		else{
			$cache_data = $this->template->view('helpcenter/helpcenter_mobile.tpl', $data,TRUE);
			$this->cache->save($cache_key, $cache_data,config_item("view_cache_time"));
		}

		echo $cache_data;

	}

	private function get_text($dom,$index)
	{
		if(isset($dom[$index]))
		{
			return $dom[$index]->plaintext;
		}
		else
		{
			return "";
		}
	}

	public function protocol()
	{
		$this->load->library('user_agent');
		$cache_key = "help-protocol-".($this->agent->is_mobile()?"mobile":"pc");
		$this->load->driver('cache',config_item("view_cache_handle"));
		if ($cache_data = $this->cache->get($cache_key))
		{
			echo $cache_data;
			return TRUE;
		}
		$this->lang->load("website");
		$this->template->assign("content",$this->lang->line("website_protocol"));
		if($this->agent->is_mobile())
		{
			$cache_data = $this->template->view("helpcenter/protocol_mobile.tpl",'',TRUE);
		}
		else
		{
			$cache_data = $this->template->view("helpcenter/protocol.tpl",'',TRUE);
		}
		$this->cache->save($cache_key,$cache_data,config_item("view_cache_time"));
		echo $cache_data;

	}
	private function ajaxReturn($data){
		echo json_encode($data);
		exit;
	}
	public function login_help_center(){
		$this->template->view('helpcenter/login_help_center.tpl');
	}
	public function login(){
		if($this->is_login())
		{
			redirect('helpcenter/edit_help_list');
			exit;
		}
		$uname = $this->input->post('uname');
		$pwd = $this->input->post('pwd');
		if($uname=="wowostar" && $pwd=="wowostar8686")
		{
			$this->session->set_userdata('help_center_token',md5($uname.$pwd.time()));
			$this->ajaxReturn(array("suc"=>true));
		}
		else{
			$this->ajaxReturn(array("suc"=>false));
		}
	}
	public function logout(){
		$this->session->unset_userdata('help_center_token');
		redirect('helpcenter');
	}
	private function is_login(){
		$token=$this->session->userdata('help_center_token');
		return !empty($token)?true:false;
	}
	/*********以下代码上线前需要注释掉*************/
	public function edit_help_list(){
		//$token = $this->input->get('token');
		$this->load->database();
		if($this->is_login())
		{
			$result = $this->db->get('helpcenter')->result_array();
			$data = array(
				'help_data' => $result
			);
			//var_dump($data);exit;
			$this->template->view('helpcenter/edit_help_list.tpl',$data);
		}
		else
		{
			redirect('helpcenter/login_help_center');
		}

	}
	public function add_help(){
		//$token = $this->input->get('token');
		if($this->is_login())
		{
			$this->template->view('helpcenter/add_help.tpl');
		}
		else{
			redirect('helpcenter/login_help_center');
		}
	}
	
	public function run_add_help(){
		$this->load->database();
		if($this->is_login()){
			$data = array(
				'title' => $this->input->post('title'),
				'content' => $this->input->post('content')
			);
			if($this->db->insert('helpcenter', $data)){
				$this->load->driver('cache',config_item("view_cache_handle"));
				$this->cache->delete("help-index-pc");
				$this->cache->delete("help-index-mobile");
				$this->template->view('helpcenter/add_help.tpl');
			}
		}
		else{
			redirect('helpcenter/login_help_center');
		}
	}
	
	public function delete_help(){
		$this->load->database();
		if($this->is_login()) {
			$id = $this->input->get('id');
			if ($this->db->delete('helpcenter', array('id' => $id))) {
				$this->load->driver('cache',config_item("view_cache_handle"));
				$this->cache->delete("help-index-pc");
				$this->cache->delete("help-index-mobile");
				echo "delete suc [id=".$id."]";
			}
			else{
				echo "delete fail [id=".$id."]";
			}
		}
		else{
			redirect('helpcenter/login_help_center');
		}
	}
	
	public function edit_help(){
		$this->load->database();
		if($this->is_login()) {
			$id = $this->input->get('id');
			$result = $this->db->get_where('helpcenter', array('id' => $id))->row_array();
			$data = array(
				'help_data' => $result
			);
			$this->template->view('helpcenter/edit_help.tpl', $data);
		}
		else{
			redirect('helpcenter/login_help_center');
		}
	}
	
	public function run_edit_help(){
		$this->load->database();
		if($this->is_login()) {
			$id = $this->input->post('id');
			$data = array(
				'title' => $this->input->post('title'),
				'content' => $this->input->post('content')
			);
			if ($this->db->where('id', $id)->update('helpcenter', $data)) {

				$this->load->driver('cache',config_item("view_cache_handle"));
				$this->cache->delete("help-index-pc");
				$this->cache->delete("help-index-mobile");
				redirect('helpcenter');
			}
		}
		else{
			redirect('helpcenter/login_help_center');
		}
	}
	/**************************************/
	
}